package com.kai.controller;

import com.kai.pojo.User;
import com.kai.service.UserService;
import com.kai.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@RestController
public class LoginController {

    @Autowired
    private UserService userService;


    /*验证码*/
    @RequestMapping("/getCode")
    @CrossOrigin
    public String getCode(HttpSession session){
        //获取验证码图片
        BufferedImage imageCode = CodeUtils.imageCode();
        //CodeUtils.getCodeText())：验证码字符
        session.setAttribute("IMG_CODE",CodeUtils.getCodeText());
        String imageToBase64 = null;
        try {
            imageToBase64 = ImageToBase64.imageToBase64(imageCode);
            return imageToBase64;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    /*登陆*/
    @RequestMapping("/login")
    public HashMap<String, Object> login(HttpSession session, @RequestBody User user) {
        HashMap<String, Object> map = new HashMap<>();
        User selectUser = null;
        String userPassword = user.getPassword();
        String s = MD5Utils.MD5(userPassword);
        user.setPassword(s);
        String imgCode = (String) session.getAttribute("IMG_CODE");
        System.out.println(user.getCode());
        System.out.println(imgCode);
        if (user.getCode().equalsIgnoreCase(imgCode)) {
            selectUser = userService.selectUser(user);
            if (selectUser != null) {
                session.setAttribute("loginUser",selectUser);
                session.setAttribute("token",selectUser.getId());
                map.put("loginUser",selectUser);
                map.put("token",selectUser.getId());
                map.put("msg","ok");
                if (selectUser.getGrade()==0){
                    map.put("path","/home");
                    return map;
                }else {
                    map.put("path","/index");
                    return map;
                }
            }else {
                map.put("msg","账号或密码错误");
                return map;
            }
        }else {
            map.put("msg","验证码错误");
            return map;
        }
    }
    /**
     * 获取session
     * @param session
     * @return
     */
    @RequestMapping("/getSession")
    public Boolean getSession(@RequestHeader("token") String token, HttpSession session){
        User loginUser = (User)session.getAttribute("loginUser");
        if (loginUser==null){
            return false;
        }else{
            if (loginUser.getId().equals(token)){
                return true;
            }else{
                return false;
            }
        }
    }

    /**
     * 退出
     * @param session
     * @return
     */
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("userInfo");
        return "ok";
    }

    /*注册*/
    @PostMapping("/register")
    public Map register(@RequestBody User user){
        HashMap<String, String> map = new HashMap<>();
        String userName = user.getName();
        int i = userService.selectUserByName(userName);
        if (i>0){
            map.put("msg","用户已存在");
            return map;
        }else{
            user.setCreatetime(new Date());
            user.setId(UUIDUtils.getUUID());
            user.setPassword(MD5Utils.MD5(user.getPassword()));
            userService.register(user);
            map.put("msg","注册成功");
            map.put("path","/login");
            return map;
        }
    }

    /*判断用户是否存在*/
    @GetMapping("/validateName")
    public int validateName(String name){
        return userService.validateName(name);
    }

}
